home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
simulato
/
v2_3_mc6.tz
/
v2_3_mc6
/
testfiles
/
treefnd1.asm
< prev
next >
Wrap
Assembly Source File
|
1994-05-02
|
11KB
|
257 lines
************************************************************
*
* CDA 3101. FALL '92.
* Assn #2.
* Inserts, searches, prints elements in a binary
* search tree. Program prompts the user for a command
* (I,S,P,E) and a single digit (0-9) and either inserts
* the value in tree, searches for the value in tree,
* prints the tree, or exits.
*
*
************************************************************
*
*********************************
* ANCHOR
*********************************
*
ORG $2000
ANCHOR DS.L 1
*
*********************************
* MESSAGES
*********************************
*
INTEGER DC.B 'Integer ' ;for printing purposes
PRN_VAL DS.B 1 ;bogus location for word operations
VALUE DS.B 1 ;integer value is stored here
DC.B ': ',0
INSERTED DC.B ' has been inserted.',13,10,13,10,0
EXISTS DC.B ' already exists in this tree.',13,10,0
FOUND DC.B ' has been found in this tree.',13,10,13,10,0
NOT DC.B ' could not be found in this tree.',13,10,0
ERROR DC.B 'Only enter valid commands (I,S,P, or E)',13,10,0
EMPTY DC.B 'Tree is empty.',13,10,0
PROMPT1 DC.B 'Enter command (I,S,P, or E) and integer (0-9) ...',13,10,0
PRN_PROMPT DC.B 'Traversal in preorder ...',13,10,0
CHAR_RTN DC.B 13,10,0 ;used only to make output more aesthetic
*
*********************************************
*
* M A I N P R O G R A M
*
*********************************************
*
ORG $1000
MAIN CLR.L D0 ;
MOVE.L D0,ANCHOR ;initialize ANCHOR
START MOVEA.L #PROMPT1,A0 ;prompt the user
JSR WriteString ;
MOVEA.L #$6000,A0 ;
JSR ReadString ;
MOVEA.L #$6000,A0 ;
JSR FIND_INFO ;get command and integer value
CMP.B #'I',D0 ;if command = I, jump to INSERT
BNE SECOND
JSR INSERT
BRA START
SECOND CMP.B #'S',D0 ;if command = S, jump to SEARCH
BNE THIRD
JSR SEARCH
BRA START
THIRD CMP.B #'P',D0 ;if command = P, jump to PRINTOUT
BNE FOURTH
JSR PRINTOUT
BRA START
FOURTH CMP.B #'E',D0 ;if command = E, jump to EXIT
BNE LAST
JSR EXIT
LAST MOVEA.L #ERROR,A0 ;if command is invalid, print
JSR WriteString ; error message
BRA START
*
********************************************
*
* SUBROUTINE: F I N D _ I N F O
*
********************************************
*
FIND_INFO MOVEA.L A0,A3 ;copy the register
HEREF CMP.B #$20,(A3) ;is A3 pointing to a blank space?
BNE CONTF ; if not, this is the command
LEA 1(A3),A3 ; if so, increment A3 and check
BRA HEREF
CONTF MOVE.B (A3),D0 ;put command into D0
JSR LOW_TO_UP ;change from lower to upper case
CMP.B #'I',D0 ;if the command is an I or an S,
BEQ GET_NUM ; then look for integer value
CMP.B #'S',D0 ; needed
BEQ GET_NUM ;if command is no I or S, return
BRA RETURNF
GET_NUM LEA 1(A3),A3 ;increment A3
CMP.B #$20,(A3) ;is A3 pointing to a blank space?
BNE GOT_NUM ; if not, this is the int val
BRA GET_NUM ; if so, go back
GOT_NUM MOVE.B (A3),VALUE ;put integer value into VALUE
RETURNF RTS
*
********************************************
*
* SUBROUTINE: L O W _ T O _ U P
*
********************************************
*
LOW_TO_UP CMP.B #'i',D0 ;only look for valid commands
BNE 1$ ; in lower case to convert to
MOVE.B #'I',D0 ; upper case
BRA RETURNL
1$ CMP.B #'s',D0
BNE 2$
MOVE.B #'S',D0
BRA RETURNL
2$ CMP.B #'p',D0
BNE 3$
MOVE.B #'P',D0
BRA RETURNL
3$ CMP.B #'e',D0
BNE RETURNL
MOVE.B #'E',D0
RETURNL RTS
*
********************************************
*
* SUBROUTINE: I N S E R T
*
********************************************
*
INSERT MOVEA.L #CHAR_RTN,A0 ;aesthetics
JSR WriteString
MOVEA.L #INTEGER,A0 ;message 'integer' + integer val
JSR WriteString
MOVE.L #10,D0 ;need 10 bytes for new node
JSR malloc
MOVE.L #0,(A0) ;initialize new node with null
MOVE.W #$FF,4(A0) ; left and right pointers and
MOVE.L #0,6(A0) ; and $FF for data
CMP.L #0,(ANCHOR).L ;is the ANCHOR null?
BNE CONTI ; if not, branch
MOVE.L A0,ANCHOR ; if so, give ANCHOR its value
MOVE.W PRN_VAL,4(A0) ; and put the value in
BRA DONEI
CONTI MOVEA.L ANCHOR,A4 ;copy the anchor
HEREI MOVE.W 4(A4),D3 ;
CMP.B VALUE,D3 ;is the value already in the tree
BEQ EXISTI ; if so, branch
BMI RIGHTI ;else, is value < data field
LEFTI CMPI.L #0,(A4) ;if the left pointer is null
BEQ LINSERT ; branch
MOVEA.L (A4),A4 ;else, go down to left node
BRA HEREI ; and check again
LINSERT MOVE.L A0,(A4) ;make left pointer = new node
MOVEA.L (A4),A4 ;move to new node
MOVE.W PRN_VAL,4(A4) ;put value into data field
BRA DONEI
RIGHTI CMPI.L #0,6(A4) ;if the right pointer is null
BEQ RINSERT ; branch
MOVEA.L 6(A4),A4 ;else, go down to right node
BRA HEREI ; and check again
RINSERT MOVE.L A0,6(A4) ;make right pointer = new node
MOVEA.L 6(A4),A4 ;move to new node
MOVE.W PRN_VAL,4(A4) ;put value into data field
BRA DONEI
EXISTI MOVEA.L #EXISTS,A0 ;message
JSR WriteString
BRA RETURNI
DONEI MOVEA.L #INSERTED,A0 ;message
JSR WriteString
RETURNI RTS
*
********************************************
*
* SUBROUTINE: S E A R C H
*
********************************************
*
SEARCH MOVEA.L #CHAR_RTN,A0 ;aesthetics
JSR WriteString
MOVEA.L #INTEGER,A0 ;message
JSR WriteString
CMP.L #0,ANCHOR ;is the ANCHOR null
BEQ EMPTYS ; if so, branch (tree empty)
MOVEA.L ANCHOR,A4 ; if not, copy ANCHOR
HERES MOVE.W 4(A4),D3
CMP.B VALUE,D3 ;is value = data field
BEQ FOUNDS ; if so, element is found
BMI RIGHTS ;is value < data field
LEFTS CMPI.L #0,(A4) ;are you at a leaf node
BEQ NOTS ; if so, element not found
MOVEA.L (A4),A4 ; if not, move to left node
BRA HERES ; and check again
RIGHTS CMPI.L #0,6(A4) ;are you at a leaf node
BEQ NOTS ; if so, element not found
MOVEA.L 6(A4),A4 ; if not move to right node
BRA HERES ; and check again
EMPTYS MOVEA.L #EMPTY,A0 ;message
JSR WriteString
BRA RETURNS
FOUNDS MOVEA.L #FOUND,A0 ;message
JSR WriteString
BRA RETURNS
NOTS MOVEA.L #NOT,A0 ;message
JSR WriteString
RETURNS RTS
*
********************************************
*
* SUBROUTINE: P R I N T O U T
*
********************************************
*
PRINTOUT LINK A6,#-40 ;give stack max space needed
MOVEA.L #CHAR_RTN,A0 ;aesthetics
JSR WriteString
MOVEA.L #PRN_PROMPT,A0 ;message
JSR WriteString
CMP.L #0,ANCHOR ;is ANCHOR null
BEQ EMPTYP ; if so, tree is empty
MOVEA.L ANCHOR,A4 ; if not, copy ANCHOR
MOVE.L #$FF,-(A7) ;initialize end of stack
HEREP MOVE.W 4(A4),D0 ;preorder-print element first
JSR WriteChar
MOVE.W #$20,D0 ;aesthetics - spaces between
JSR WriteChar
CMP.L #0,6(A4) ;is the right child null
BEQ CONTP ; if so, no stack operation
MOVE.L 6(A4),-(A7) ; if not, put address on stack
CONTP CMP.L #0,(A4) ;is the left child null
BEQ CHECK ; if so, branch
MOVEA.L (A4),A4 ; if not, move to left child
BRA HEREP
CHECK CMP.L #$FF,(A7) ;is this the end of the stack
BEQ END ; if so, you're done
MOVEA.L (A7)+,A4 ; if not, get address from stack
BRA HEREP
EMPTYP MOVEA.L #EMPTY,A0 ;message
JSR WriteString
END MOVEA.L #CHAR_RTN,A0 ;aesthetics
JSR WriteString
MOVEA.L #CHAR_RTN,A0 ;aesthetics
JSR WriteString
UNLK A6
RTS
*
********************************************
*
* E N D O F P R O G R A M
*
********************************************
*
EXIT MOVE #228,D7 ;
TRAP #14 ;
NOLIST
DC.W 0
INCLUDE "samples.asm"
LIST
END